﻿using System.Text;
using System.Web;

namespace ServBus.ListForm2010.Utility
{
    public class LogManager
    {
        public static void AppendToPopTxt(HttpResponse Response, string errorStr)
        {
            Response.AddHeader("Content-Disposition", "attachment; filename=error.log");
            Response.ContentType = "text/plain";
            Response.ContentEncoding = Encoding.UTF8;
            Response.Write(errorStr);
            Response.End();
        }

        public static void AppendErrorHtml(HttpResponse Response, string errorUrl)
        {
//            uriContent = "data:application/octet-stream;filename=filename.txt," + 
//              encodeURIComponent(codeMirror.getValue());
//newWindow=window.open(uriContent, 'filename.txt');
            string errorStr = "<script type=\"text/javascript\">"
                            + "function download(){"
                            + "var uriContent = \"data:application/octet-stream;filename=errorlog.txt,\" +"
                            + "encodeURIComponent(codeMirror.getValue());"
                            + "a.window.close();"
                            + "a.close();"
                            + "}"
                            + "</script>";
            Response.Write(errorStr);
        }

        public static void downloadLogFile(HttpResponse Response, string errorMessage)
        {
            Response.ClearHeaders();
            Response.Clear();
            Response.Expires = 0;
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=error.log");
            Response.ContentType = "Application/octet-stream";
            Response.Write(errorMessage);
            Response.End();
        }
    }
}
